﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ArcannEngine.Graphics
{
    public class ModificationsBox
    {
        private Vector2 pos;
        private int offsetX, offsetY;
        private SpriteFont font;
        private bool organized = false;
        private SpriteBatch spriteBatch;


        private List<Modificator> modificators;
        public List<Modificator> Modificators
        {
            get
            {
                return modificators;
            }
        }

        public ModificationsBox(Vector2 pos, int offsetX, int offsetY, SpriteFont font, SpriteBatch spriteBatch)
        {
            this.pos = pos;
            this.offsetX = offsetX;
            this.offsetY = offsetY;
            this.font = font;
            this.spriteBatch = spriteBatch;
            modificators = new List<Modificator>();
        }

        public bool Add(Modificator mod)
        {
            bool success = false;
            bool found = false;

            int i = 0;
            foreach (Modificator mod_ in modificators)
            {
                if (modificators[i] != mod)
                {
                }
                else
                {
                    found = true;
                    break;
                }
            }
            if (found == true)
            {
            }
            else
            {
                modificators.Add(mod);
                this.organized = false;
                success = true;
            }
            return success;
        }

        public void Organize()
        {
            int i = 0;

            float startX = pos.X + offsetX/2;
            float startY = pos.Y + offsetY;

            Vector2 cursor = new Vector2();
            cursor.X = startX;
            cursor.Y = startY;

            while (i < modificators.Count)
            {
                modificators[i].descPos = cursor;
                cursor.X += offsetX * 3f;
                modificators[i].minus.X = (int)cursor.X;
                modificators[i].minus.Y = (int)cursor.Y;
                cursor.X += modificators[i].minus.Width + offsetX * 1.2f - font.MeasureString(modificators[i].Target + "").X / 2;
                modificators[i].targetPos = cursor;
                cursor.X = modificators[i].minus.X + modificators[i].minus.Width + 2.5f * offsetX;
                modificators[i].plus.X = (int)cursor.X;
                modificators[i].plus.Y = (int)cursor.Y;
                cursor.X += modificators[i].plus.Width + 15;
                modificators[i].unitPos = cursor;

                cursor.X = startX;
                cursor.Y += offsetY;
                i++;
            }
            organized = true;
        }

        public void Draw()
        {
            if (organized == false)
            {
                this.Organize();
            }
            else //draw
            {
                foreach (Modificator mod in modificators)
                {
                    spriteBatch.Draw(mod.tex, mod.minus, Color.Chocolate);
                    spriteBatch.Draw(mod.tex, mod.plus, Color.Chocolate);
                    spriteBatch.DrawString(font, mod.desc, mod.descPos, Color.White);
                    spriteBatch.DrawString(font, mod.Target + "", mod.targetPos, Color.White);
                    spriteBatch.DrawString(font, mod.unit, mod.unitPos, Color.White);
                    spriteBatch.DrawString(font, "-", new Vector2(mod.minus.X+10, mod.minus.Y+5), Color.White);
                    spriteBatch.DrawString(font, "+", new Vector2(mod.plus.X + 7, mod.plus.Y + 5), Color.White);
                }
            }
        }
    }
}
